package fr.efrei.tradesys.treatments;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;

public class DBLoader
{

	private static final String	DRIVER_NAME	= "oracle.jdbc.driver.OracleDriver";

	static
	{
		try
		{
			Class.forName(DRIVER_NAME).newInstance();
			System.out.println("*** Driver loaded");
		}
		catch (Exception e)
		{
			System.out.println("*** Error : " + e.toString());
			System.out.println("*** ");
			System.out.println("*** Error : ");
			e.printStackTrace();
		}
	}

	public void resetDatabase(String fileName) throws SQLException,
			NamingException
	{
		final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
		final String USER = "tradesys";
		final String PASSWORD = "tradesys";
		String s = new String();
		StringBuffer sb = new StringBuffer();
		String stmt = "";

		FileReader fr;
		try
		{
			fr = new FileReader(new File(fileName));
			BufferedReader br = new BufferedReader(fr);
			while ((s = br.readLine()) != null)
			{
				if (!s.startsWith("/*"))
					sb.append(s);
			}
			br.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		String[] stmts = sb.toString().split(";");
		for (int i = 0; i < stmts.length; i++)
		{
			if (stmts[i].contains("BEGIN"))
			{
				int tInt = i;
				while (tInt < stmts.length && !stmts[tInt].contains("END"))
				{
					tInt++;
				}
				stmts[i] += ";";
				for (int j = (i + 1); j < tInt; j++)
				{
					stmts[i] += "\n" + stmts[j] + ";";
					stmts[j] = " ";
				}
				stmts[i] += "\nEND";
				String tStr[] = stmts[tInt].split("END");
				stmts[tInt] = tStr[1];
				i = tInt - 1;
			}
		}
		Connection c = DriverManager.getConnection(URL, USER, PASSWORD);
		Statement st = c.createStatement();
		for (int i = 0; i < stmts.length; i++)
		{
			if (!stmts[i].trim().equals(""))
			{
				stmt = stmts[i];
				System.out.println(">>" + stmts[i]);
				try
				{
					st.executeUpdate(stmt);
				}
				catch (Exception e)
				{
					System.out.println(e.toString());
				}
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			String request = "";
			String file = System.getProperty("user.home")
					+ "\\workspace\\TradeSys\\db\\Oracle_init.sql";
			InputStream ips = new FileInputStream(file);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String line;
			while ((line = br.readLine()) != null)
			{
				if (!line.startsWith("/*"))
					request += line + "\n";
			}
			br.close();
			Class.forName("oracle.jdbc.driver.OracleDriver");
			new DBLoader().resetDatabase(file);
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (NamingException e)
		{
			e.printStackTrace();
		}
	}

}
